﻿Imports System.Net.Mail
Public Class Mailer
    Private Sub Mailer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        password.UseSystemPasswordChar = True
    End Sub
    Private Sub butsend_Click(sender As Object, e As EventArgs) Handles butsend.Click
        If My.Computer.Network.IsAvailable = True Then
            Dim smtpserver As New SmtpClient
            If username.Text = "" Or password.Text = "" Then
                MsgBox("Enter the username and password!", vbCritical, "Express Mail client v2.0 - Error")
            Else
                smtpserver.Credentials = New Net.NetworkCredential(username.Text, password.Text)
                Select Case cmboserver.SelectedItem.Text
                    Case "GMail"
                        smtpserver.Port = 465
                        smtpserver.Host = "smtp.gmail.com"
                    Case "Outlook.com"
                        smtpserver.Port = 587
                        smtpserver.Host = "smtp.live.com"
                    Case "Office365.com"
                        smtpserver.Port = 587
                        smtpserver.Host = "smtp.office365.com"
                    Case "Yahoo! Mail"
                        smtpserver.Port = 465
                        smtpserver.Host = "smtp.mail.yahoo.com"
                    Case "Yahoo! Mail Plus"
                        smtpserver.Port = 465
                        smtpserver.Host = "plus.smtp.mail.yahoo.com"
                    Case "Yahoo! UK"
                        smtpserver.Port = 465
                        smtpserver.Host = "smtp.mail.yahoo.co.uk"
                    Case "Hotmail"
                        smtpserver.Port = 465
                        smtpserver.Host = "smtp.live.com"
                    Case "Mail.com"
                        smtpserver.Port = 587
                        smtpserver.Host = "smtp.mail.com"
                End Select
            End If
            Try
                Dim Smtp_Server As New SmtpClient
                Dim e_mail As New MailMessage()
                Smtp_Server.UseDefaultCredentials = False
                Smtp_Server.EnableSsl = True
                e_mail = New MailMessage()
                e_mail.From = New MailAddress(username.Text)
                e_mail.To.Add(destination.Text)
                e_mail.Subject = "Email Sending"
                e_mail.IsBodyHtml = False
                e_mail.Body = body.Text
                Smtp_Server.Send(e_mail)
                MsgBox("Mail Sent", vbInformation, "Success - Express Mail Client")

            Catch error_t As Exception
                MsgBox(error_t.ToString)
            End Try
        Else
            MsgBox("Network is not connected. A mail cannot be sent", vbExclamation, "No network - Express Mail Client v 2.0")
        End If
    End Sub
End Class